package nc.appcan.sp.servlet;

import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import nc.appcan.sp.bs.tool.AnnotateTool;
import nc.bs.framework.adaptor.IHttpServletAdaptor;
import nc.bs.framework.common.NCLocator;
import nc.bs.logging.Logger;
import nc.bs.pub.pf.PfUtilTools;
import nc.itf.uap.pf.INCMAFileInter;
import nc.itf.uap.pf.IWorkflowDefine;

public class AppcanWorkflowServlet extends HttpServlet implements IHttpServletAdaptor {

	private static final long serialVersionUID = 591955759856323777L;



	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doAction(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doAction(req, resp);
	}

	public void doAction(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Logger.init("appcansp");
		OutputStream out = null;
		try{		
				String billid =	request.getParameter("billid");
				String billtype  =request.getParameter("billtype");
				PfUtilTools.regDataSourceForServlet(request.getHeader("accountcode"));
				IWorkflowDefine  workflow = NCLocator.getInstance().lookup(	IWorkflowDefine.class);
		     	byte[] flowbyte = workflow.toPNGImage(billid, billtype, 1);
		     	response.getOutputStream().write(flowbyte);
		   	
			}catch(Exception e){
				Logger.error(e.getMessage(), e);
				e.printStackTrace();
			}finally{
				try {
					if (out != null) {
						out.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		
	}
	
}
